<?php
/**
 * Description of MainQuestionclass
 *
 * @author Chisheng Lu, Jeffy Shih
 * @copyright Copyright(c)2010 DoRISE team, Science Education center, NTNU.
 * @since 2010-03-09
 * @version 1.1
 */
class MainQuestion {
    //put your code here
    private $mysqli;

    function __construct() {
        //引入資料庫連結參數設定
        include '../inc/oaConn.inc.php';
        $this->mysqli = @new mysqli($mysqlHost,$mysqlUser,$mysqlPassword,$mysqlDB);
        if(mysqli_connect_errno()) {
            throw new Exception('無法建立資料庫連線！'.mysqli_connect_error());
            $this->mysqli = false;
            exit();
        }

        //連線使用編碼
        $this->mysqli->query("SET NAMES 'utf8'");
    }

    /**
     *
     * @param Array $aryMainQuestion
     * @return Integer Return Insert MainQuestion ID
     */
    public function addMainQuestion($aryMainQuestion) {
        $intRtn = 0;
        $strNow = $this->getNowTime();
        $sql_addMQ = 'INSERT INTO `MainQuestion` (';
        $sql_values = ' VALUES(';
        if (array_key_exists('MQTitle', $aryMainQuestion)) {
            $sql_addMQ .= '`MQTitle`, ';
            $sql_values .= '\'' . $aryMainQuestion['MQTitle'] . '\', ';
        }
        if (array_key_exists('SId', $aryMainQuestion)) {
            $sql_addMQ .= '`SId`, ';
            $sql_values .= $aryMainQuestion['SId'] . ', ';
        }
        if (array_key_exists('TId', $aryMainQuestion)) {
            $sql_addMQ .= '`TId`, ';
            $sql_values .= $aryMainQuestion['TId'] . ', ';
        }
        if (array_key_exists('MQBelief', $aryMainQuestion)) {
            $sql_addMQ .= '`MQBelief`, ';
            $sql_values .= '\'' . $aryMainQuestion['MQBelief'] . '\', ';
        }
        if (array_key_exists('MQPaper', $aryMainQuestion)) {
            $sql_addMQ .= '`MQPaper`, ';
            $sql_values .= $aryMainQuestion['MQPaper'] . ', ';
        }
        if (array_key_exists('MQGradeBegin', $aryMainQuestion)) {
            $sql_addMQ .= '`MQGradeBegin`, ';
            $sql_values .= $aryMainQuestion['MQGradeBegin'] . ', ';
        }
        if (array_key_exists('MQGradeEnd', $aryMainQuestion)) {
            $sql_addMQ .= '`MQGradeEnd`, ';
            $sql_values .= $aryMainQuestion['MQGradeEnd'] . ', ';
        }
        if (array_key_exists('MQContent', $aryMainQuestion)) {
            $sql_addMQ .= '`MQContent`, ';
            $sql_values .= '\'' . $aryMainQuestion['MQContent'] . '\', ';
        }
        if (array_key_exists('UserId', $aryMainQuestion)) {
            $sql_addMQ .= '`UserId`, ';
            $sql_values .= $aryMainQuestion['UserId'] . ', ';
        }
        $sql_addMQ .= '`MQCreated`';
        $sql_values .= '\'' . $strNow . '\') ';
        $sql_addMQ .= ')' . $sql_values;
        $rs_addMQ = $this->mysqli->query($sql_addMQ);
        if (!$rs_addMQ) {
            throw new Exception('新增題幹資料時發生錯誤！'.$sql_addMQ);
        }else {
            $intRtn = $this->mysqli->insert_id;
        }
        return $intRtn;

    }

    /**
     *
     * @param Intger $intMQId
     * @param Array $aryMainQuestion
     * @return Boolean
     */
    public function editMainQuestion($intMQId, $aryMainQuestion) {
        //檢查題幹編號是否為空白
        if (empty ($intMQId)) {
            throw new Exception('缺少題幹編號！');
        }
        $bolRtn = false;
        $strNow = $this->getNowTime();
        $sql_editMQ = 'Update MainQuestion set ';
        if (array_key_exists('MQTitle', $aryMainQuestion))
            $sql_editMQ .= 'MQTitle = \'' . $aryMainQuestion['MQTitle'] . '\', ';
        if (array_key_exists('SId', $aryMainQuestion))
            $sql_editMQ .= 'SId = ' . $aryMainQuestion['SId'] . ', ';
        if (array_key_exists('TId', $aryMainQuestion))
            $sql_editMQ .= 'TId = ' . $aryMainQuestion['TId'] . ', ';
        if (array_key_exists('MQBelief', $aryMainQuestion))
            $sql_editMQ .= 'MQBelief = \'' . $aryMainQuestion['MQBelief'] . '\', ';
        if (array_key_exists('MQPaper', $aryMainQuestion))
            $sql_editMQ .= 'MQPaper = ' . $aryMainQuestion['MQPaper'] . ', ';
        if (array_key_exists('MQGradeBegin', $aryMainQuestion))
            $sql_editMQ .= 'MQGradeBegin = ' . $aryMainQuestion['MQGradeBegin'] . ', ';
        if (array_key_exists('MQGradeEnd', $aryMainQuestion))
            $sql_editMQ .= 'MQGradeEnd = ' . $aryMainQuestion['MQGradeEnd'] . ', ';
        if (array_key_exists('MQContent', $aryMainQuestion))
            $sql_editMQ .= 'MQContent = \'' . $aryMainQuestion['MQContent'] . '\', ';
        if (array_key_exists('UserId', $aryMainQuestion))
            $sql_editMQ .= 'UserId = ' . $aryMainQuestion['UserId'] . ', ';
        $sql_editMQ .= 'MQModified = \'' . $strNow . '\' ';
        $sql_editMQ .= 'where MQId = ' . $intMQId . ';';
        $rs_editMQ = $this->mysqli->query($sql_editMQ);
        if (!$rs_editMQ) {
            throw new Exception('更新題幹資料時發生錯誤！');
        }else {
            $bolRtn = true;
        }
        return $bolRtn;
    }

    /**
     *
     * @param Integer $intMQId
     * @return Boolean
     */
    public function delMainQuestion($intMQId) {
        $bolRtn = false;
        //檢查題幹編號是否為空白
        if (empty ($intMQId)) {
            throw new Exception('錯誤！缺少題幹編號。');
        }

        //刪除
        $sql_delMQ = 'DELETE FROM `MainQuestion` WHERE `MQId`=' . $intMQId;
        $rs_delMQ = $this->mysqli->query($sql_delMQ);
        if (!$rs_delMQ) {
            throw new Exception('錯誤！刪除題幹時發生錯誤。');
        }else {
            $bolRtn = true;
        }

        return $bolRtn;

    }

    /**
     *
     * @param Integer $intMQId
     * @return Array
     */
    public function getMainQuestionInfoByMQId($intMQId) {
        //檢查題幹編號是否為空白
        if (empty ($intMQId)) {
            throw new Exception('缺少題幹編號！');
        }

        $sql_getMQById = 'SELECT * FROM `MainQuestion` WHERE `MQId`=' . $intMQId;
        $rs_getMQById = $this->mysqli->query($sql_getMQById);

        if (!$rs_getMQById) {
            throw new Exception('無法查詢該題幹的資料！');
        }else {
            if ($rs_getMQById->num_rows == 0) {
                throw new Exception('無此題幹編號的資料！');
            }else {
                $row_getMQById = $rs_getMQById->fetch_row();
                return $row_getMQById;
            }
        } //End If

    }

    /**
     *
     * @return Array
     */
    public function getMainQuestionList() {
        $sql_getMQList = 'SELECT * FROM `MainQuestion`';
        $rs_getMQList = $this->mysqli->query($sql_getMQList);

        if (!$rs_getMQList) {
            throw new Exception('無法查詢該題幹的資料！');
        }else {
            $aryMQList = array();
            while ($aryOneMQ = $rs_getMQList->fetch_row()) {
                array_push($aryMQList, $aryOneMQ);
            }
            return $aryMQList;
        } //End If

    }

    /**
     *
     * @param Integer $intUserId
     * @return Array
     * @author Jeffy Shih
     * @since Version 1.1 (2010-06-02)
     */

    public function getMainQuestionListByUserId($intUserId) {
        //檢查使用者編號是否為空白
        if (empty ($intUserId)) {
            throw new Exception('缺少使用者編號！');
        }

        $sql_getMQList = 'SELECT * FROM `MainQuestion` WHERE `UserId`=' . $intUserId;
        $rs_getMQList = $this->mysqli->query($sql_getMQList);

        if (!$rs_getMQList) {
            throw new Exception('無法查詢該題幹的資料！');
        }else {
            $aryMQList = array();
            while ($aryOneMQ = $rs_getMQList->fetch_row()) {
                array_push($aryMQList, $aryOneMQ);
            }
            return $aryMQList;
        } //End If
    }

    /**
     *
     * @return String
     */
    private function getNowTime() {
        return date('Y-m-d H:i:s');
    }

    function  __destruct() {
        if ($this->mysqli) {
            $this->mysqli->close();
            $this->mysqli = FALSE;
        }
    }
}
?>
